﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HsTec.DigitalPlatform.DataAccess
{
    public class UserInfoRepository
    {
        DigitalPlatformEntities db = new DigitalPlatformEntities();

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="userinfo"></param>
        public void Add(UserInfo userinfo)
        {
            db.AddToUserInfos(userinfo);
            db.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="userinfo"></param>
        public void Delete(UserInfo userinfo)
        {
            db.DeleteObject(userinfo);
            db.SaveChanges();
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            db.SaveChanges();
        }

        /// <summary>
        /// 根据ID查询实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserInfo GetUserInfoByID(int id)
        {
            return db.UserInfos.FirstOrDefault<UserInfo>(d => d.userid == id);
        }

        /// <summary>
        /// 返回所有实体
        /// </summary>
        /// <returns></returns>
        public IQueryable<UserInfo> FindALLUserInfo()
        {
            return db.UserInfos;
        }

        /// <summary>
        /// 登录账户检查
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <returns></returns>
        public bool CheckUserInfo(string userName, string passWord)
        {
            UserInfo userinfo = db.UserInfos.FirstOrDefault<UserInfo>(d => d.username.Equals(userName) && d.password.Equals(passWord));
            if (userinfo != null)
            {
                userinfo.lastlogindate = DateTime.Now;
                db.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

        public IQueryable<UserInfo> PageInfo(int page, int rp, string sortname, string sortorder)
        {
            return db.UserInfos.OrderBy("it." + sortname + " " + sortorder).Skip((page - 1) * rp).Take(rp);
        }

        public int GetAllCount()
        {
            return db.UserInfos.Count();
        }
    }
}
